<template>
  <el-dialog title="重新分配经纪人"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-radio-group v-model="agentId">
      <el-radio v-for="(item, index) in agentList" :label="item.id" :key="index">{{item.name}}</el-radio>
    </el-radio-group>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    name: 'distributeAgent',
    data () {
      return {
        visible: false,
        agentList: [],
        agentId: '',
        customerList: []
      }
    },
    methods: {
      init (list) {
        this.visible = true
        this.customerList = []
        list.forEach((item) => {
          this.customerList.push(item.id)
        })
        this.agentId = ''
        this.getAgentList()
      },
      dataFormSubmit () {
        if(!this.agentId){
          this.$message.error('请选择经纪人')
          return
        }
        this.$http({
          url: this.$http.adornUrl('/user/batch/match/agentId'),
          method: 'post',
          data: this.$http.adornData({
            agentId: this.agentId,
            ids: this.customerList
          })
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        })
      },
      getAgentList () {
        this.$http({
          url: this.$http.adornUrl('/agent/page'),
          method: 'get',
          params: this.$http.adornParams(Object.assign({
            current: 1,
            size: 1000
          }, {}))
        }).then(({ data }) => {
          this.agentList = data.records
        })
      },
    }
  }
</script>

<style scoped>

</style>
